<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');

class Flex_seqs extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->model('sys/flex_seqs_model');
		$this->load->library('variable');
		$this->load->library('PHPExcel');

	}

	public function index(){
	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'));
	  $this->load->view('layouts/footer');
	 }

	 public function add_form(){
		$this->load->view($this->session->userdata('foradd'));
	}

	public function edit_form($id){
		$this->load->view($this->session->userdata('foredit'),array(
				'flex_seqs' => $this->flex_seqs_model->get_flex_seqs_id($id)
			));
	}

	public function view_form($id){
		$this->load->view($this->session->userdata('forview'));
	}

	public function get_status() {
		echo json_encode($this->flex_seqs_model->get_status($this->input->post('tbl_name'),$this->input->post('fld_name')));
	}

	public function insert(){

		$data = array(
				'p_table_name'	=>	$this->input->post('table_name'),
				'p_field_name'	=>	$this->input->post('table_col'),
				'p_separator'	=>	$this->input->post('separator'),
				'p_user_id'		=>	$this->session->userdata('user_id')
			);

		echo json_encode($this->flex_seqs_model->insert($data));

	}

	public function insertseg(){

		$data = array(
				'p_flex_seq_id'		=>	$this->input->post('flex_seq_id'),
				'p_seqment_seq'		=>	$this->input->post('segment_seq'),
				'p_seqment_length'	=>	$this->input->post('segment_length'),
				'p_seqment_name'	=>	$this->input->post('segment_name'),
				'p_source_type'		=>	$this->input->post('source_type'),
				'p_source_name'		=>	$this->input->post('source_name'),
				'p_default_values'	=>	$this->input->post('default_values'),
				'p_editable_flag'	=>	($this->input->post('editable_flag') == 'Y' ? 'Y' : 'N'),
				'p_user_id'			=>	$this->session->userdata('user_id')
			);

		echo json_encode($this->flex_seqs_model->insertseg($data));

	}

	public function updateseg(){

		$data = array(
				'p_flex_seq_id'		=>	$this->input->post('segment_id'),
				'p_seqment_seq'		=>	$this->input->post('segment_seq'),
				'p_seqment_length'	=>	$this->input->post('segment_length'),
				'p_seqment_name'	=>	$this->input->post('segment_name'),
				'p_source_type'		=>	$this->input->post('source_type'),
				'p_source_name'		=>	$this->input->post('source_name'),
				'p_default_values'	=>	$this->input->post('default_values'),
				'p_editable_flag'	=>	($this->input->post('editable_flag') == 'Y' ? 'Y' : 'N'),
				'p_user_id'			=>	$this->session->userdata('user_id')
			);

		echo json_encode($this->flex_seqs_model->updateseg($data));

	}

	public function del_seg(){

		$data = array(
				'p_flex_seq_id'		=>	$this->input->post('id')
			);

		echo json_encode($this->flex_seqs_model->del_seg($data));

	}

	public function get_tables(){
		echo json_encode($this->flex_seqs_model->get_tables());
	}
  	
  	public function get_table_col(){
		echo json_encode($this->flex_seqs_model->get_table_col($this->input->post('table_name')));
	}

	public function get_lookup_values(){
		echo json_encode($this->flex_seqs_model->get_lookup_values('SOURCE_TYPE'));
	}

	public function get_app_seq(){
		echo json_encode($this->flex_seqs_model->get_app_seq('app_sequence'));
	}

	public function get_field_name(){
		echo json_encode($this->flex_seqs_model->get_field_name($this->input->post('id')));
	}

	public function get_segments(){
		echo json_encode($this->flex_seqs_model->get_segments($this->input->post('id')));
	}

	public function get_segments_by_id(){
		echo json_encode($this->flex_seqs_model->get_segments_by_id($this->input->post('id')));
	}

	public function get_flex_seqs(){
		$table_name = $this->input->post('table_name');
		$table_field = $this->input->post('table_field');
		$result = $this->flex_seqs_model->get_flex_seqs($table_name, $table_field);
		$separator = '';
		$char = '';
		$tmp = '';
		$val = array();
		foreach($result as $res){
			$separator = $res->separator;
			$result2 = $this->flex_seqs_model->get_segments($res->flex_seq_id);
			foreach ($result2 as $res2) {
				switch ($res2->source_type) {
					case 'FORMULA':
						$string  = explode('|', $res2->default_value);
						foreach($string as $s){
							if($s == '{sep}'){
								$tmp = $res->separator;
							}else{
								$tmp = $this->input->post($s);
							}
							$char .= $tmp;
						}
						array_push($val,array('value' => $char,
						'seg_seq'                     => $res2->segment_seq,
						'seg_length'                  => $res2->segment_length,
						'seg_name'                    => str_replace(' ', '_', strtolower($res2->segment_name)),
						'editable_flag'               => $res2->editable_flag,
											  'separator' => $res->separator));
						break;
					case 'MANUAL':
						array_push($val,array('value' =>$res2->default_value, 
											  'seg_seq' => $res2->segment_seq,
											  'seg_length' => $res2->segment_length,
											  'seg_name' => str_replace(' ', '_', strtolower($res2->segment_name)),
											  'editable_flag' => $res2->editable_flag,
											  'separator' => $res->separator));
						break;
					case 'SEQUENCE':
						$resu = $this->flex_seqs_model->get_sequence($res2->source_name);
						foreach($resu as $res3){
							array_push($val,array('value' => $res3->val, 
											  'seg_seq' => $res2->segment_seq,
											  'seg_length' => $res2->segment_length,
											  'seg_name' => str_replace(' ', '_', strtolower($res2->segment_name)),
											  'editable_flag' => $res2->editable_flag,
											  'separator' => $res->separator));
						}
						break;
					
					default:
						
						break;
				}
			}
		}
		echo json_encode($val);
	}

	public function get_flex_seqs_2(){
		$table_name = $this->input->post('table_name');
		$table_field = $this->input->post('table_field');
		$result = $this->flex_seqs_model->get_flex_seqs($table_name, $table_field);
		$separator = '';
		$char = '';
		$tmp = '';
		$val = array();
		foreach($result as $res){
			$separator = $res->separator;
			$result2 = $this->flex_seqs_model->get_segments($res->flex_seq_id);
			foreach ($result2 as $res2) {
				switch ($res2->source_type) {
					case 'FORMULA':
						$string  = explode('|', $res2->default_value);
						foreach($string as $s){
							if($s == '{sep}'){
								$tmp = $res->separator;
							}else{
								$tmp = $this->input->post($s);
							}
							$char .= $tmp;
						}
						array_push($val,array('value' =>$char,
						'seg_seq'                     => $res2->segment_seq,
						'seg_length'                  => $res2->segment_length,
						'seg_name'                    => str_replace(' ', '_', strtolower($res2->segment_name)),
						'editable_flag'               => $res2->editable_flag,
											  'separator' => $res->separator));
						break;
					case 'MANUAL':
						array_push($val,array('value' =>$res2->default_value, 
											  'seg_seq' => $res2->segment_seq,
											  'seg_length' => $res2->segment_length,
											  'seg_name' => str_replace(' ', '_', strtolower($res2->segment_name)),
											  'editable_flag' => $res2->editable_flag,
											  'separator' => $res->separator));
						break;
					case 'SEQUENCE':
						$resu = $this->flex_seqs_model->get_sequence($res2->source_name);
						foreach($resu as $res3){
							array_push($val,array('value' =>$res3->val, 
											  'seg_seq' => $res2->segment_seq,
											  'seg_length' => $res2->segment_length,
											  'seg_name' => str_replace(' ', '_', strtolower($res2->segment_name)),
											  'editable_flag' => $res2->editable_flag,
											  'separator' => $res->separator));
						}
						break;
					default:
						
						break;
				}
			}
		}
		echo json_encode($val);
	}

	public function get_flex_seqs_e(){
		$table_name = $this->input->post('table_name');
		$table_field = $this->input->post('table_field');
		$result = $this->flex_seqs_model->get_flex_seqs($table_name, $table_field);
		$separator = '';
		$char = '';
		$tmp = '';
		$val = array();
		foreach($result as $res){
			$separator = $res->separator;
			$result2 = $this->flex_seqs_model->get_segments($res->flex_seq_id);
			foreach ($result2 as $res2) {
				switch ($res2->source_type) {
					case 'FORMULA':
						$string  = explode('|', $res2->default_value);
						foreach($string as $s){
							if($s == '{sep}'){
								$tmp = $res->separator;
							}else{
								$tmp = $this->input->post($s);
							}
							$char .= $tmp;
						}
						array_push($val,array('value' =>$char,
						'seg_seq'                     => $res2->segment_seq,
						'seg_length'                  => $res2->segment_length,
						'seg_name'                    => str_replace(' ', '_', strtolower($res2->segment_name)),
						'editable_flag'               => $res2->editable_flag,
											  'separator' => $res->separator));
						break;
					case 'MANUAL':
						array_push($val,array('value' =>$res2->default_value, 
											  'seg_seq' => $res2->segment_seq,
											  'seg_length' => $res2->segment_length,
											  'seg_name' => str_replace(' ', '_', strtolower($res2->segment_name)),
											  'editable_flag' => $res2->editable_flag,
											  'separator' => $res->separator));
						break;
					case 'SEQUENCE':
						$resu = $this->flex_seqs_model->get_sequence($res2->source_name);
						foreach($resu as $res3){
							array_push($val,array('value' => $res3->val, 
											  'seg_seq' => $res2->segment_seq,
											  'seg_length' => $res2->segment_length,
											  'seg_name' => str_replace(' ', '_', strtolower($res2->segment_name)),
											  'editable_flag' => $res2->editable_flag,
											  'separator' => $res->separator));
						}
						break;
					default:
						
						break;
				}
			}
		}
		echo json_encode($val);
	}

	public function get_curr_value(){
		$table_name = $this->input->post('table_name');
		$table_field = $this->input->post('table_field');
		$result = $this->flex_seqs_model->get_flex_seqs($table_name, $table_field);
		$result4 = $this->flex_seqs_model->get_account($this->input->post('account_id'));
		$separator = '';
		$char = '';
		$tmp = '';
		$val = array();
		foreach($result as $res){
			$separator = $res->separator;
			$result2 = $this->flex_seqs_model->get_segments($res->flex_seq_id);
			foreach ($result2 as $res2) {
				foreach($result4 as $res4){
					switch ($res2->source_type) {
						case 'FORMULA':
							$string  = explode('|', $res2->default_value);
							foreach($string as $s){
								if($s == '{sep}'){
									$tmp = $res->separator;
								}else{
									$tmp = $this->input->post($s);
								}
								$char .= $tmp;
							}
							array_push($val,array('value' =>$char,
							'seg_seq'                     => $res2->segment_seq,
							'seg_length'                  => $res2->segment_length,
							'seg_name'                    => str_replace(' ', '_', strtolower($res2->segment_name)),
							'editable_flag'               => $res2->editable_flag,
												  'separator' => $res->separator));
							break;
						case 'MANUAL':
							array_push($val,array('value' => $res->field_name, 
												  'seg_seq' => $res2->segment_seq,
												  'seg_length' => $res2->segment_length,
												  'seg_name' => str_replace(' ', '_', strtolower($res2->segment_name)),
												  'editable_flag' => $res2->editable_flag,
												  'separator' => $res->separator));
							break;
						case 'SEQUENCE':
							$resu = $this->flex_seqs_model->get_sequence($res2->source_name);
							foreach($resu as $res3){
								array_push($val,array('value' => $res4->acct_num, 
												  'seg_seq' => $res2->segment_seq,
												  'seg_length' => $res2->segment_length,
												  'seg_name' => str_replace(' ', '_', strtolower($res2->segment_name)),
												  'editable_flag' => $res2->editable_flag,
												  'separator' => $res->separator));
							}
							break;
						
						default:
							
							break;
					}
				}
			}
		}
		echo json_encode($val);
	}
}
